﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using VmroleAuthor.Helpers;
using VmroleAuthor.Models;
using VmroleAuthor.ViewModels;

namespace VmroleAuthor.Views
{
    /// <summary>
    /// Interaction logic for SaveAsFileWindow.xaml
    /// </summary>
    public partial class SaveAsFileWindow : UserControl
    {
        private readonly string prevSaveAsFilePath;    

        public SaveAsFileWindow(string prevSaveAsFilePath, string prevWorkingDirectory)
        {
            InitializeComponent();
            this.prevSaveAsFilePath = prevSaveAsFilePath;          
            FileLocation.Text = prevSaveAsFilePath;
            WorkingDir.Text = prevWorkingDirectory;
            
        }

        public const string SaveAsFileWindowName = "SaveAsFileWindow";
      
        public bool DataValid { get; private set; }        
    
        private void BrowseFile(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog
                {
                    DefaultExt = ".resextpkg",
                    Filter = "Resource Extension Package (*.resextpkg)|*.resextpkg|Resource Definition Package (*.resdefpkg)|*.resdefpkg | Resource Definition (*.resdef)|*.resdef"
                };

            dlg.Title = "Save To A New Location";  
            if (this.prevSaveAsFilePath != null)
            {
                dlg.InitialDirectory = System.IO.Path.GetDirectoryName(this.prevSaveAsFilePath);
            }            
            bool? result = dlg.ShowDialog();
            // Process open file dialog box results 
            if (result == true)
            {               
                if (dlg.FileName != null)
                {
                    FileLocation.Text = dlg.FileName;                   

                    string dirName = System.IO.Path.GetDirectoryName(dlg.FileName);
                    if (!string.IsNullOrEmpty(dirName))
                    {
                        string newFolderName = System.IO.Path.GetFileNameWithoutExtension(dlg.FileName);
                        if (string.IsNullOrEmpty(newFolderName))
                        {
                            newFolderName = "GeneratedFolder";
                        }
                        WorkingDir.Text = System.IO.Path.Combine(dirName, newFolderName);
                    }                
                }               
            }
        }

        private void OkButton_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(this.FileLocation.Text) || string.IsNullOrEmpty(this.WorkingDir.Text))
            {
                MessageBox.Show("File Location or Working Directory is invalid", DefinitionViewModel.ToolHeader);
                return;
            }
            if (!System.IO.Path.IsPathRooted(this.FileLocation.Text) || !System.IO.Path.IsPathRooted(this.WorkingDir.Text))
            {
                MessageBox.Show("File Location or Working Directory is non-existance", DefinitionViewModel.ToolHeader);
                return;
            }          
            this.DataValid = true;
            Window win = Application.Current.Windows.OfType<Window>().SingleOrDefault(w => w.Name == SaveAsFileWindowName);
            if (win != null) win.Close(); 
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DataValid = false;
            Window win = Application.Current.Windows.OfType<Window>().SingleOrDefault(w => w.Name == SaveAsFileWindowName);
            if (win != null) win.Close();           
        }
    }
}
